﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfStudy
{
    /// <summary>
    /// Interaction logic for StudyDataView.xaml
    /// </summary>
    public partial class StudyDataView : Window
    {
        private ListCollectionView view;
        public StudyDataView()
        {
            InitializeComponent();
            CustomerService service = new CustomerService();
            this.DataContext = service.GetAllCustomers();

            cmbList.ItemsSource = service.GetAllCustomers();

            view = CollectionViewSource.GetDefaultView(this.DataContext) as ListCollectionView;
            view.CurrentChanged += view_CurrentChanged;

            view.Filter = new Predicate<object>(FilterName);

            
        }

        private bool FilterName(object obj)
        {
            Customer customer = obj as Customer;
            return customer.Name == "Jason";
        }

        void view_CurrentChanged(object sender, EventArgs e)
        {
            txtPosition.Text = "Record " + (view.CurrentPosition + 1).ToString() + " of " + view.Count.ToString();
            btnPre.IsEnabled = view.CurrentPosition > 0;
            btnNext.IsEnabled = view.CurrentPosition < view.Count - 1;
        }

        private void btnPre_Click_1(object sender, RoutedEventArgs e)
        {
            view.MoveCurrentToPrevious();
        }

        private void btnNext_Click_1(object sender, RoutedEventArgs e)
        {
            view.MoveCurrentToNext();
        }

        private void cmbList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        { 
            view.MoveCurrentTo(cmbList.SelectedItem);
        }
    }
}
